# -*- coding: utf-8 -*-
import json

from NetEaseSpider.spiders.list import SongList

__author__ = 'wuliao'
#根据用户的id爬取数据

import threadpool
import requests
import lxml.etree
from gevent import thread
import pymongo
from NetEaseSpider.spiders.user import user
from NetEaseSpider.spiders.comment import comment

url = 'http://music.163.com/user/home?id='
client = pymongo.MongoClient('175.102.18.112', 27018)

headers = {
    'Cookie': 'vjuids=-6235acc0.157e051a56e.0.f31ae7d6d480e; _ntes_nuid=0424300be9661c79a21aebdc25a5b71c; playliststatus=visible; __gads=ID=e66b01075c2b8a03:T=1478593490:S=ALNI_MZ_ONhecfmu8sqhP7lpVbat7KE2CA; mail_psc_fingerprint=3079002c88b2606f325489767e9c3531; NTES_CMT_USER_INFO=38517579%7Cwuliao518%7Chttps%3A%2F%2Fsimg.ws.126.net%2Fe%2Fmimg.126.net%2Fp%2Fbutter%2F1008031648%2Fimg%2Fface_big.gif.39x39.100.jpg%7Cfalse%7Cd3VsaWFvNTE4QDE2My5jb20%3D; SID=75772c05-98e4-40c4-ad0a-39959fec2eb1; _qddaz=QD.9x4kvj.gew50o.j6zu16mb; _ntes_nnid=0424300be9661c79a21aebdc25a5b71c,1508477267821; __utmc=187553192; __oc_uuid=a0f7dc40-a594-11e6-b7c0-87fd672202f4; _ngd_tid=Sz6sJWn%2B58TdIhxhBG5H2Z1Mb8HEifq0; __utmc=94650624; usertrack=ezq0pVoT+Dw0fQIJA2QJAg==; _ga=GA1.2.44547508.1477469279; __q_=1; s_n_f_l_n3=2d91f04edbf36be41498197012107; _iuqxldmzr_=32; __e_=1513736151634; playerid=52995099; P_INFO=wuliao518@163.com|1515407053|1|open|00&99|shh&1515149396&mail163#shh&null#10#0#0|&0|mail163|wuliao518@163.com; starttime=; vjlast=1476937230.1516069951.11; ne_analysis_trace_id=1516069951061; vinfo_n_f_l_n3=2d91f04edbf36be4.1.14.1476937230158.1498194412548.1516069968885; __utmz=187553192.1516156935.118.90.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; NTES_SESS=99xPiATKkMn9eCQ5KzovOKKTsGkMnijO38Jr7eyWvEjB.sOFw.kOHbPzWRQTwsQkd98bzby3hJkZt.E5ioiMw1lJDk7Mrg.l6EkeLnJOS1yYzH_jw2qsnlc4iYLFAmKmpr7vr81F.orvzqL89mOGhW5fcb0DjNdiwRob0b7o9sVpD6GyiJw7u2d1T; S_INFO=1516253971|1|0&0##|wuliao518; ANTICSRF=23dbb601cf376443f8f648ab19784507; MUSIC_EMAIL_U=bc4f48def8d74a8335eafbd0b0949920734fae71b5e88f856f8756bbc52ab314f170953f68b55ce838242f6dec71fe706aaed1e4b9faf17d8bafcdfe5ad2b092; __utma=187553192.44547508.1477469279.1516330014.1516585073.121; __utma=94650624.1752173904.1478080765.1514197542.1516589887.48; __utmz=94650624.1516589887.48.7.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; JSESSIONID-WYYY=A1iil8BIMGCMXUJ4YE2%5C8vnf7Dvvox91FCtMVBQr2GTlzmJIXKEN2glXnOUHOfzQK%5CbfJ6xjis4O0eAQqhkRdkJBJS%2BYpcpf%2BBVkN5yPVJPzdFHsAPbnaP6MvlBGniNKnKJHIpF%2BU%5C4yM%2Bstd3VbA5pG%5CA7Oze3WXqG3e%2BWnT1%2BSGXeY%3A1516593426321; __utmb=94650624.15.10.1516589887',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
    'Referer': 'http://music.163.com/'
}


def load_data(intt):

    data = json.loads(user(intt).get_data())

    print data

    # if len(data["playlist"]) > 0:
    #
    #     response = requests.get(url+str(intt),headers=headers)
    #
    #     doc = lxml.etree.HTML(response.text)

        #用户信息
        # userInfo = {}
        # intro = doc.xpath("//*[@id=\"head-box\"]/dd/div[2]/text()")[0]
        # userInfo['intro'] = intro
        # nickname = doc.xpath("//*[@id=\"j-name-wrap\"]/span[1]/text()")[0]
        # userInfo['nickname'] = nickname
        # level = doc.xpath("//*[@id=\"j-name-wrap\"]/span[2]/text()")[0]
        # userInfo['level'] = level
        # area = doc.xpath("//*[@id=\"head-box\"]/dd/div[3]/span/text()")[0]
        # userInfo['area'] = area
        # stateNum= doc.xpath("//*[@id=\"event_count\"]/text()")[0]
        # userInfo['stateNum'] = stateNum
        # attentionNum = doc.xpath("//*[@id=\"follow_count\"]/text()")[0]
        # userInfo['attentionNum'] = attentionNum
        # fansNum = doc.xpath("//*[@id=\"fan_count\"]/text()")[0]
        # userInfo['fansNum'] = fansNum
        # sex = get_sex(doc.xpath("//*[@id=\"j-name-wrap\"]/i//@class")[0])
        # userInfo['sex'] = sex
        # avatar = doc.xpath("//*[@id=\"ava\"]/img//@src")[0]
        # userInfo['avatar'] = avatar
        # id = int(intt)
        # userInfo['id'] = id
        # saveUser(userInfo)

        #user创建的歌单
        # isAll = data['more']
        # playList = data['playlist']
        # saveSongs(playList)
        # page = 0
        # while (isAll):
        #     page = page+1
        #     temp = json.loads(user(intt,page).get_data())
        #     isAll= temp['more']
        #     playList = temp['playlist']
        #     saveSongs(playList)


def saveSongs(playList):
    db_list = client.wuliao.songlist
    db_song = client.wuliao.song


    for i in playList:

        item = {}
        item['id'] = i['id']
        item['userId'] = i['userId']
        item['updateTime'] = i['updateTime']
        item['coverImgUrl'] = i['coverImgUrl']
        item['name'] = i['name']
        item['tags'] = i['tags']

        db_item = db_list.find({'id': i['id']})

        if db_item.count() == 0:#未记录歌单
            data = SongList(i['id']).get_data()

            for it in data:
                song_item = db_song.find({'id': it['id']})
                if song_item.count() == 0:
                    db_song.save(it)
            #获取评论
            # saveComment(it['id'])

def saveSongList(id):
    db = client.wuliao.songlist




def saveComment(id):
    db = client.wuliao.comment

    for i in json.loads(comment(id).get_data())['comments']:
        # 判断commentId
        item = {}
        item['content'] = i['content']
        item['userId'] = i['user']['userId']
        item['likeCount'] = i['likedCount']
        item['commentId'] = i['commentId']
        item['time'] = i['time']

        db_item = db.find({'commentId': item['commentId']})
        if db_item.count() == 0:
            db.save(item)









def saveUser(user):
    db = client.wuliao.user

    db_item = db.find({'id': user['id']})
    if db_item.count() == 0:
        db.save(user)


def get_sex(args):
    if args.find("u-icn-01")!=-1:
        return "男"
    elif args.find("u-icn-02")!=-1:
        return "女"
    else:
        return "未知"




data = [its for its in range(1, 2)]
pool = threadpool.ThreadPool(1)
i_lock = thread.allocate_lock()
request = threadpool.makeRequests(load_data, data)
[pool.putRequest(req) for req in request]
pool.wait()





